<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content="Apache Forrest" name="Generator">
    <meta name="Forrest-version" content="0.9">
    <meta name="Forrest-skin-name" content="pelt">
    <title>Streaming with BookKeeper</title>
    <link type="text/css" href="skin/basic.css" rel="stylesheet">
    <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
    <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
    <link type="text/css" href="skin/profile.css" rel="stylesheet">
    <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script>
    <script src="skin/getMenu.js" language="javascript" type="text/javascript"></script>
    <script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
    <link rel="shortcut icon" href="images/favicon.ico">
</head>
<body onload="init()">
<script type="text/javascript">ndeSetTextSize();</script>
<div id="top">
    <!--+
        |breadtrail
        +-->
    <div class="breadtrail">
        <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://zookeeper.apache.org/">ZooKeeper</a>
        &gt; <a href="http://zookeeper.apache.org/">ZooKeeper</a>
        <script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
    </div>
    <!--+
        |header
        +-->
    <div class="header">
        <!--+
            |start group logo
            +-->
        <div class="grouplogo">
            <a href="http://hadoop.apache.org/"><img class="logoImage" alt="Hadoop"
                                                     src="images/hadoop-logo.jpg"
                                                     title="Apache Hadoop"></a>
        </div>
        <!--+
            |end group logo
            +-->
        <!--+
            |start Project Logo
            +-->
        <div class="projectlogo">
            <a href="http://zookeeper.apache.org/"><img class="logoImage" alt="ZooKeeper"
                                                        src="images/zookeeper_small.gif"
                                                        title="ZooKeeper: distributed coordination"></a>
        </div>
        <!--+
            |end Project Logo
            +-->
        <!--+
            |start Search
            +-->
        <div class="searchbox">
            <form action="http://www.google.com/search" method="get" class="roundtopsmall">
                <input value="zookeeper.apache.org" name="sitesearch" type="hidden"><input
                    onFocus="getBlank (this, 'Search the site with google');" size="25" name="q"
                    id="query" type="text" value="Search the site with google">&nbsp;
                <input name="Search" value="Search" type="submit">
            </form>
        </div>
        <!--+
            |end search
            +-->
        <!--+
            |start Tabs
            +-->
        <ul id="tabs">
            <li>
                <a class="unselected" href="http://zookeeper.apache.org/">Project</a>
            </li>
            <li>
                <a class="unselected" href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/">Wiki</a>
            </li>
            <li class="current">
                <a class="selected" href="index.html">ZooKeeper 3.4 Documentation</a>
            </li>
        </ul>
        <!--+
            |end Tabs
            +-->
    </div>
</div>
<div id="main">
    <div id="publishedStrip">
        <!--+
            |start Subtabs
            +-->
        <div id="level2tabs"></div>
        <!--+
            |end Endtabs
            +-->
        <script type="text/javascript"><!--
        document.write("Last Published: " + document.lastModified);
        //  --></script>
    </div>
    <!--+
        |breadtrail
        +-->
    <div class="breadtrail">

        &nbsp;
    </div>
    <!--+
        |start Menu, mainarea
        +-->
    <!--+
        |start Menu
        +-->
    <div id="menu">
        <div onclick="SwitchMenu('menu_1.1', 'skin/')" id="menu_1.1Title" class="menutitle">
            Overview
        </div>
        <div id="menu_1.1" class="menuitemgroup">
            <div class="menuitem">
                <a href="index.html">Welcome</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperOver.html">Overview</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperStarted.html">Getting Started</a>
            </div>
            <div class="menuitem">
                <a href="releasenotes.html">Release Notes</a>
            </div>
        </div>
        <div onclick="SwitchMenu('menu_1.2', 'skin/')" id="menu_1.2Title" class="menutitle">
            Developer
        </div>
        <div id="menu_1.2" class="menuitemgroup">
            <div class="menuitem">
                <a href="api/index.html">API Docs</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperProgrammers.html">Programmer's Guide</a>
            </div>
            <div class="menuitem">
                <a href="javaExample.html">Java Example</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperTutorial.html">Barrier and Queue Tutorial</a>
            </div>
            <div class="menuitem">
                <a href="recipes.html">Recipes</a>
            </div>
        </div>
        <div onclick="SwitchMenu('menu_1.3', 'skin/')" id="menu_1.3Title" class="menutitle">
            BookKeeper
        </div>
        <div id="menu_1.3" class="menuitemgroup">
            <div class="menuitem">
                <a href="bookkeeperStarted.html">Getting started</a>
            </div>
            <div class="menuitem">
                <a href="bookkeeperOverview.html">Overview</a>
            </div>
            <div class="menuitem">
                <a href="bookkeeperConfig.html">Setup guide</a>
            </div>
            <div class="menuitem">
                <a href="bookkeeperProgrammer.html">Programmer's guide</a>
            </div>
        </div>
        <div onclick="SwitchMenu('menu_1.4', 'skin/')" id="menu_1.4Title" class="menutitle">Admin
            &amp; Ops
        </div>
        <div id="menu_1.4" class="menuitemgroup">
            <div class="menuitem">
                <a href="zookeeperAdmin.html">Administrator's Guide</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperQuotas.html">Quota Guide</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperJMX.html">JMX</a>
            </div>
            <div class="menuitem">
                <a href="zookeeperObservers.html">Observers Guide</a>
            </div>
        </div>
        <div onclick="SwitchMenu('menu_1.5', 'skin/')" id="menu_1.5Title" class="menutitle">
            Contributor
        </div>
        <div id="menu_1.5" class="menuitemgroup">
            <div class="menuitem">
                <a href="zookeeperInternals.html">ZooKeeper Internals</a>
            </div>
        </div>
        <div onclick="SwitchMenu('menu_1.6', 'skin/')" id="menu_1.6Title" class="menutitle">
            Miscellaneous
        </div>
        <div id="menu_1.6" class="menuitemgroup">
            <div class="menuitem">
                <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER">Wiki</a>
            </div>
            <div class="menuitem">
                <a href="https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ">FAQ</a>
            </div>
            <div class="menuitem">
                <a href="http://zookeeper.apache.org/mailing_lists.html">Mailing Lists</a>
            </div>
        </div>
        <div id="credit"></div>
        <div id="roundbottom">
            <img style="display: none" class="corner" height="15" width="15" alt=""
                 src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
        <!--+
          |alternative credits
          +-->
        <div id="credit2"></div>
    </div>
    <!--+
        |end Menu
        +-->
    <!--+
        |start content
        +-->
    <div id="content">
        <div title="Portable Document Format" class="pdflink">
            <a class="dida" href="bookkeeperStream.pdf"><img alt="PDF -icon"
                                                             src="skin/images/pdfdoc.gif"
                                                             class="skin"><br>
                PDF</a>
        </div>
        <h1>Streaming with BookKeeper</h1>
        <div id="front-matter">
            <div id="minitoc-area">
                <ul class="minitoc">
                    <li>
                        <a href="#bk_StreamSummary">Summary</a>
                    </li>
                    <li>
                        <a href="#bk_LedgerOutputStream">Writing a stream of bytes</a>
                    </li>
                    <li>
                        <a href="#bk_LedgerInputStream">Reading a stream of bytes</a>
                    </li>
                </ul>
            </div>
        </div>


        <a name="bk_StreamSummary"></a>
        <h2 class="h3">Summary</h2>
        <div class="section">
            <p>
                When using the BookKeeper API, an application has to split the data to write into
                entries, each
                entry being a byte array. This is natural for many applications. For example, when
                using BookKeeper
                for write-ahead logging, an application typically wants to write the modifications
                corresponding
                to a command or a transaction. Some other applications, however, might not have a
                natural boundary
                for entries, and may prefer to write and read streams of bytes. This is exactly the
                purpose of the
                stream API we have implemented on top of BookKeeper.
            </p>
            <p>
                The stream API is implemented in the package <span class="codefrag computeroutput">Streaming</span>,
                and it contains two main classes: <span class="codefrag computeroutput">LedgerOutputStream</span>
                and
                <span class="codefrag computeroutput">LedgerInputStream</span>. The class names are
                indicative of what they do.
            </p>
        </div>


        <a name="bk_LedgerOutputStream"></a>
        <h2 class="h3">Writing a stream of bytes</h2>
        <div class="section">
            <p>
                Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two
                constructors and five public methods:
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public LedgerOutputStream(LedgerHandle lh) 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">lh</span> is a ledger handle for a
                        previously created and open ledger.
                    </p>

                </li>

            </ul>
            <p>
    
<span class="codefrag computeroutput">
	public LedgerOutputStream(LedgerHandle lh, int size) 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">lh</span> is a ledger handle for a
                        previously created and open ledger.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">size</span> is the size of the byte
                        buffer to store written bytes before flushing.
                    </p>

                </li>

            </ul>
            <p>

                <strong>Closing a stream.</strong> This call closes the stream by flushing the write
                buffer.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public void close() 
    </span>

            </p>
            <p>
                which has no parameters.
            </p>
            <p>

                <strong>Flushing a stream.</strong> This call essentially flushes the write buffer.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized void flush() 
    </span>

            </p>
            <p>
                which has no parameters.
            </p>
            <p>

                <strong>Writing bytes.</strong> There are three calls for writing bytes to a stream.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized void write(byte[] b) 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">b</span> is an array of bytes to
                        write.
                    </p>

                </li>

            </ul>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized void write(byte[] b, int off, int len) 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">b</span> is an array of bytes to
                        write.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">off</span> is a buffer offset.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">len</span> is the length to write.
                    </p>

                </li>

            </ul>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized void write(int b) 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">b</span> contains a byte to write. The
                        method writes the least significant byte of the integer four bytes.
                    </p>

                </li>

            </ul>
        </div>


        <a name="bk_LedgerInputStream"></a>
        <h2 class="h3">Reading a stream of bytes</h2>
        <div class="section">
            <p>
                Class <span class="codefrag computeroutput">LedgerOutputStream</span> implements two
                constructors and four public methods:
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public LedgerInputStream(LedgerHandle lh)
	throws BKException, InterruptedException 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">lh</span> is a ledger handle for a
                        previously created and open ledger.
                    </p>

                </li>

            </ul>
            <p>
    
<span class="codefrag computeroutput">
	public LedgerInputStream(LedgerHandle lh, int size)  throws BKException, InterruptedException
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">lh</span> is a ledger handle for a
                        previously created and open ledger.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">size</span> is the size of the byte
                        buffer to store bytes that the application
                        will eventually read.
                    </p>

                </li>

            </ul>
            <p>

                <strong>Closing.</strong> There is one call to close an input stream, but the call
                is currently empty and the application is responsible for closing the ledger handle.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public void close()
    </span>

            </p>
            <p>
                which has no parameters.
            </p>
            <p>

                <strong>Reading.</strong> There are three calls to read from the stream.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized int read()
	throws IOException 
    </span>

            </p>
            <p>
                which has no parameters.
            </p>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized int read(byte[] b)
	throws IOException 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">b</span> is a byte array to write to.
                    </p>

                </li>

            </ul>
            <p>
    
<span class="codefrag computeroutput">
	public synchronized int read(byte[] b, int off, int len)
	throws IOException 
    </span>

            </p>
            <p>
                where:
            </p>
            <ul>

                <li>

                    <p>

                        <span class="codefrag computeroutput">b</span> is a byte array to write to.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">off</span> is an offset for byte array
                        <span class="codefrag computeroutput">b</span>.
                    </p>

                </li>


                <li>

                    <p>

                        <span class="codefrag computeroutput">len</span> is the length in bytes to
                        write to <span class="codefrag computeroutput">b</span>.
                    </p>

                </li>

            </ul>
        </div>

        <p align="right">
            <font size="-2"></font>
        </p>
    </div>
    <!--+
        |end content
        +-->
    <div class="clearboth">&nbsp;</div>
</div>
<div id="footer">
    <!--+
        |start bottomstrip
        +-->
    <div class="lastmodified">
        <script type="text/javascript"><!--
        document.write("Last Published: " + document.lastModified);
        //  --></script>
    </div>
    <div class="copyright">
        Copyright &copy;
        <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
    </div>
    <!--+
        |end bottomstrip
        +-->
</div>
</body>
</html>
